package com.nobug.run;

import com.nobug.MainRunSimple;
import com.nobug.MyRobot;

import java.awt.event.KeyEvent;
import java.util.Random;

/**
 *
 * 神武钓鱼 全自动钓鱼
 * @author 389561407@qq.com
 * @version 1.0
 * @since 2023-02-02
 */
public class GoFishing {

    private static int[] xxyx; //全局定位
    private static int[] imageLButton; //抛竿定位

    public static void runMain() {
        System.out.println("钓鱼系统启动，请先将鼠标放置在休闲小游戏图标上");
        System.out.println("钓鱼系统启动，请先将鼠标放置在休闲小游戏图标上");
        System.out.println("钓鱼系统启动，请先将鼠标放置在休闲小游戏图标上");
        one:
        while (true){
            if(xxyx==null){
                findAllxy();
            }

            if(imageLButton==null){
                pgdw();
            }else {
                System.out.println("移动到抛竿位置点击抛竿");
                MyRobot.move(imageLButton[0]+15,imageLButton[1]+15);
                sleep(500);
                MyRobot.lButton();
            }
            Random random = new Random();
            int i = random.nextInt(2000)+500;
            sleep(i);
            MyRobot.lButton();
            System.out.println("完成抛竿，等待上钩，启动监测上钩");

            long l = System.currentTimeMillis()+5*60*1000; //五分钟不上钩子 就跳过循环

            int[] imageLButton1 = MainRunSimple.findImage("sg1.png");
            while (imageLButton1[0]!=0&&imageLButton1[1]!=0){
                System.out.println("钓鱼监测中，请勿移动鼠标，本次已等待"+((System.currentTimeMillis()+5*60*1000-l)/1000)+"秒钟");
                if(System.currentTimeMillis()>l){
                    continue one;
                }
                imageLButton1 = MainRunSimple.findImage("sg1.png");
            }
            System.out.println("上钩！！！！！！！！！");
            MyRobot.move(imageLButton[0]+62,imageLButton[1]);
            sleep(500);

            pullRod();
            System.out.println("完成拉杆，展示结果，准备收取");

            MyRobot.move(xxyx[0]+400,xxyx[1]+196);
            sleep(500);
            MainRunSimple.findImageLButton("sq.png");
            System.out.println("完成收取");

        }

    }

    //找到游戏全局定位
    private static void findAllxy() {
        //找到休闲游戏位置
        int flag = 0;
        do {
            if(flag>10){
                System.out.println("定位失败，退出程序");
                return;
            }
            xxyx = MainRunSimple.findImage("simple.png");
            flag++;
        }while (xxyx[0]==0&&xxyx[1]==0);
        System.out.println("成功定位,请将鼠标移动至抛竿上，无需其他操作");
        System.out.println("成功定位,请将鼠标移动至抛竿上，无需其他操作");
        System.out.println("成功定位,请将鼠标移动至抛竿上，无需其他操作");
        sleep(5000);
    }

    private static void pgdw() {
        //找到休闲游戏位置
        int flag = 0;
        do {
            if(flag>10){
                System.out.println("定位失败，退出程序");
                System.exit(0);
            }
            imageLButton = MainRunSimple.findImage("pg1.png");
            flag++;
        }while (imageLButton[0]==0&&imageLButton[1]==0);
        System.out.println("成功定位,开始自动钓鱼");
        System.out.println("成功定位,开始自动钓鱼");
        System.out.println("成功定位,开始自动钓鱼");
        sleep(500);
        MyRobot.lButton();
    }

    private static void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void pullRod() {
//        System.out.println("程序启动1秒后启动，正式的去掉");
//        sleep(1000);
        System.out.println("点击叹号开始拉杆");
        MyRobot.lButton();
        sleep(1500);
        MyRobot.keyTime(KeyEvent.VK_SPACE,1300);
        System.out.println("放开");
        sleep(1000);
        System.out.println("按下");
        MyRobot.keyTime(KeyEvent.VK_SPACE,800);

        System.out.println("放开");
        sleep(700);
        System.out.println("按下");
        MyRobot.keyTime(KeyEvent.VK_SPACE,3000);

        System.out.println("放开");
        sleep(700);
        System.out.println("按下");
        MyRobot.keyTime(KeyEvent.VK_SPACE,2500);

        System.out.println("放开");
        sleep(700);
        System.out.println("按下");
        MyRobot.keyTime(KeyEvent.VK_SPACE,500);

        System.out.println("放开");
        sleep(500);
        System.out.println("按下");
        MyRobot.keyTime(KeyEvent.VK_SPACE,600);

        System.out.println("放开");
        sleep(500);
        System.out.println("按下");
        MyRobot.keyTime(KeyEvent.VK_SPACE,600);

        System.out.println("放开");
        sleep(500);
        System.out.println("按下");
        MyRobot.keyTime(KeyEvent.VK_SPACE,600);
    }
}
